'use strict';
const Controller = require('egg').Controller;
const md5 = require('md5');
const mongoose = require('mongoose');
class OtherController extends Controller {  
    
    async requeOther(){
        const {ctx,service} = this;
        if(ctx.session.admin && ctx.session.islogin){
            let { name } = ctx.request.query;
            let result = await ctx.model.Other.findOne({name: name});
            ctx.body = result;
        }else{
            ctx.body = {data:0,content:'暂未登录'}
        }
    }

    async getone(){
        const {ctx,service} = this;
        let json = ctx.request.query;
        let result = await service.normal.getinfo('Other',json);
        ctx.body = result;
    }

    async updateone(){
        const {ctx,service} = this;
        let {name,array} = ctx.request.body;
        let result = await service.normal.statebyjson('Other',{name},{array});
        ctx.body = result;
    }

    async upAdminTab(){
        const {ctx,service} = this;
        try {
            let {sid,json,array} = ctx.request.body;
            let result = await ctx.model.AdminTab.findOne({ userId: sid })
            if(result){
                let results = await service.normal.updateinfobyid('AdminTab',result._id,json);
                ctx.body = results;
                return null;
            }else{
                let results = await ctx.model.Admin.findOne({ _id: mongoose.mongo.ObjectId(sid) })
                let dd = {
                    userId : results._id,
                    title : results.nickName + "侧边栏",
                    phone : results.phone,
                    nickName : results.nickName,
                    level : results.level,
                    array: array
                }
                let resultss = await service.normal.addinfo('AdminTab',dd);
                ctx.body = resultss;
            }

        } catch (error) {
            ctx.body = {data:0,error}
        }
    }
  
}

module.exports = OtherController;
